using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Athanor.Gui;
using Athanor.Utils;

namespace Athanor
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Athanor : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        Grid grid = new Grid();

        public Athanor()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            graphics.SynchronizeWithVerticalRetrace = false;
            graphics.ApplyChanges();

            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //Independent debug
            Components.Add(new DebugComponent(this));

            InputManager.Init();
            ControllerManager.Init();
            //GameStateManager.Init();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            ResourcesManager.GetInstance().Init(Content, graphics);

            GuiManager.GetInstance().Init();
            //CameraManager.Init();
            //CameraManager.GetInstance().CreateCamera(Camera.ECamType.ECAMERA_TYPE_FREE, true, null, new Vector3(0, 0, -1000), new Vector3(0, 0, 0));
            //GameStateManager.GetInstance().ChangeState(new GameStateSinglePlayer());

            //grid.Start();

            // TODO: use this.Content to load your game content here
            GuiManager.GetInstance().AddComponent("bg", new Sprite(0, 0, "background01_level01_"));

            AnimatedSprite as1 = new AnimatedSprite();
            as1.AddAnimation("correr", new Animation("rail/rail_corre", 2, 6, 12));
            as1.AddAnimation("espera", new Animation("rail/rail_esp", 4, 4, 16));
            GuiManager.GetInstance().AddComponent("rail", as1);
            as1.setAnimation("correr");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || 
                Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();


            InputManager.GetInstance().Update(gameTime);
            GuiManager.GetInstance().Update(gameTime);
            //CameraManager.GetInstance().Update(gameTime);
            //GameStateManager.GetInstance().Update(gameTime);

            AnimatedSprite as1 = (AnimatedSprite)GuiManager.GetInstance().GetSprite("rail");
            if (ControllerManager.GetInstance().isPressed(0, "move_right"))
            {
                as1.Effects = SpriteEffects.FlipHorizontally;
                as1.setAnimation("correr");
            }
            else if (ControllerManager.GetInstance().isPressed(0, "move_left"))
            {
                as1.Effects = SpriteEffects.None;
                as1.setAnimation("correr");
            }
            else if (!ControllerManager.GetInstance().isDown(0, "move_right") && !ControllerManager.GetInstance().isDown(0, "move_left"))
            {
                as1.setAnimation("espera");
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            //grid.Draw(gameTime);
            GuiManager.GetInstance().Draw(gameTime);
            //GameStateManager.GetInstance().Draw(gameTime);

            base.Draw(gameTime);
        }

        static void Main(string[] args)
        {
            using (
                Athanor game = new Athanor())
            {
                game.Run();
            }
        }
    }
}
